/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.ajax;

import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.panel.FormPanel;

public class FormRequest extends SimpleRequest {

	protected JavaScript validError = new JavaScript();

	protected FormPanel form;
	
	public FormRequest(String url, FormPanel form) {
		super(METHOD_POST, url);
		setForm(form);
	}

	public FormRequest(String url, FormPanel form, IWait wait) {
		super(METHOD_POST, url);
		setForm(form);
		setWait(wait);
	}
	
	public void setForm(FormPanel form) {
		this.form = form;
		setFormId(form.getFormId());
	}

	public FormPanel getForm() {
		return form;
	}
	
	public void setFormId(String formId) {
		addOption("form",formId);
	}

	public String getFormId() {
		return getString("form","");
	}

	public void setValidError(JavaScript validError) {
		this.validError = validError;
	}

	public JavaScript getValidError() {
		return this.validError;
	}

	@Override
	public String getScript() {
		createItems();
		script = new StringBuffer(super.getScript());
		return new JavaScript(
				"if(Ext.getCmp('{0}').getForm().isValid()){{1}}else{{2}}",form.getId()
				, script.toString(), validError.getScript()).getScript();
	}

}
